import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:we_chat_app/const.dart';
import 'package:we_chat_app/friend/Friends.dart';
import 'package:we_chat_app/friend/friend_cell.dart';

class PeopleList extends StatefulWidget {



  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return PeopleListState();
  }




}

class PeopleListState extends State<PeopleList> {

  List<Widget> wordsWidget = [];

  Map<String,double> indexMap = {};

  late ScrollController _scrollController;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    for (var value1 in indexWorld) {
      wordsWidget.add(Container(
        height: 20,
        child: Text(value1,style: TextStyle(fontSize: 10,color: Colors.grey),),
      ));
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    List<Widget> list = [
      FriendCell('images/xinpengyou.png', '新的朋友', ''),
      FriendCell('images/qunliao.png', '群聊', ''),
      FriendCell('images/gongzhonghao.png', '公众号', ''),
      FriendCell('images/biaoqian.png', '标签', ''),
    ];
    String tempLetter = '';
    int index = 0;
    for(var i=0;i<datas.length;i++) {
      var value = datas[i];
      if(tempLetter != value.indexLetter) {
        indexMap[value.indexLetter] = i*60 + index * 10 + 40;
        index += 1;
        tempLetter = value.indexLetter;
        list.add(Text(value.indexLetter));
      }
      list.add(FriendCell(value.headImage, value.name, value.indexLetter));
    }



    return Stack(
        children: [
          Container(
            color: contextColor,
            child: ListView(
              children: list,
              controller: _scrollController,
          ),),
          Positioned(
              right: 20,
              top: 70,
              child: Row(
                children: [
                  GestureDetector(
                  onVerticalDragUpdate: (DragUpdateDetails details){
                    double y = details.localPosition.dy;
                    double x = details.localPosition.dx;
                    if(x>0 && x <10)  {
                      if(y~/20<indexWorld.length) {
                        double? height = indexMap[indexWorld[y~/20]];
                        if(height!=null) {
                          _scrollController.animateTo(height, duration: Duration(microseconds: 200), curve: Curves.easeIn);
                        }

                      }
                    }
                  },
                  onVerticalDragDown: (DragDownDetails details) {
                    print(details);
                  },
                  onVerticalDragEnd: (DragEndDetails details){
                    print('1111111111111111');
                  },
                  child: Column(
                    children: wordsWidget,
                  ),
                )
                ],
              )
          )
        ]
    );
  }

}